<?php
include_once( "common.php" );

# Output header stuff
header( "Content-Type: application/rss+xml" );
$domain = DOMAIN;
print <<<EOF
<?xml version="1.0"?>
<rss version="2.0" xmlns:atom = "http://www.w3.org/2005/Atom">
<channel>
<atom:link href = "{$domain}rss.php" rel = "self" type = "application/rss+xml" />
<title>BREaST</title>
<link>{$domain}main.php</link>
<description>Bug Reporting, Eliminating, and Sorting Tool</description>
<language>en-us</language>

EOF;

# Get all the entries this user is allowed to see
$_SERVER["QUERY_STRING"] = addslashes( $_SERVER["QUERY_STRING"] );
$result = db_query( "SELECT Log.* FROM Log WHERE Log.bug IN ( SELECT Bug.id FROM Bug WHERE Bug.project IN ( SELECT UserProject.project FROM UserProject, User WHERE UserProject.user = User.id AND User.feed = '{$_SERVER["QUERY_STRING"]}' ) ) ORDER BY timestamp DESC LIMIT 50" );

# Set up an array to map column entries to table names
$table_match = array();
$table_match["project"] = "project";
$table_match["assigned"] = "user";
$table_match["priority"] = "priority";
$table_match["status"] = "status";

# Output log entries
while ( $row = db_fetch_object( $result ) )
{
    $timestamp = formatTimestamp( $row->timestamp );

    # Get information on the bug
    $result_bug = db_query( "SELECT * FROM Bug WHERE Bug.id = {$row->bug}" );
    $row_bug = db_fetch_object( $result_bug );

    # Do lookups on the bug info
    $row->user = getNameByID( "User", $row->user );
    $row->comment = stripslashes( $row->comment );
    $row_bug->project = getNameByID( "Project", $row_bug->project );
    $row_bug->assigned = getNameByID( "Assigned", $row_bug->assigned );
    $row_bug->priority = getNameByID( "Priority", $row_bug->priority );
    $row_bug->status = getNameByID( "Status", $row_bug->status );
    $row_bug->description = stripslashes( $row_bug->description );

    # A bug was opened 
    if ( $row->initial == 1 )
    {
        # Write the info to the feed
        print "<item><title>Bug #{$row_bug->id} opened in {$row_bug->project}</title><description>\n";
        print "&lt;p&gt;Opened by {$row->user} on {$timestamp}:&lt;/p&gt;\n";
        print "&lt;p&gt;{$row_bug->description}&lt;/p&gt;\n";
        print "&lt;a href = '{$domain}index.php?id={$row_bug->id}'&gt;View bug&lt;/a&gt;</description></item>\n";
    }

    # A bug was updated
    else
    {
        # Write the info to the feed
        print "<item><title>Bug #{$row_bug->id} updated in {$row_bug->project}</title><description>\n";
        print "&lt;p&gt;Updated by {$row->user} on {$timestamp}:&lt;/p&gt;\n";
        print "&lt;p&gt;{$row_bug->description}&lt;/p&gt;\n";

        # Output any changes
        foreach ( array_keys( $table_match ) as $field )
        {
            if ( $row->$field != -1 )
            {
                print "&lt;p&gt;" . ucfirst( $field ) . ": " . getNameByID( ucfirst( $table_match[$field] ), $row->$field ) . "&lt;/p&gt;\n";
            }
        }

        # Output comments
        if ( $row->comment != "" )
        {
            print "&lt;p&gt;Comment: " . $row->comment . "&lt;/p&gt;\n";
        }

        print "&lt;a href = '{$domain}index.php?id={$row_bug->id}'&gt;View bug&lt;/a&gt;</description></item>\n";
    }
}

# Output footer stuff
print "</channel></rss>";
?>
